<?php


namespace App\backend\models;

use App\common\models\BaseModel;
use Illuminate\Support\Facades\DB;

class LifeFreeLogModel extends BaseModel
{
    const table = 'activity_lifefree_log';

    static $_instance;

    const _field = ['*'];

    public static function getInstance()
    {
        if (!(self::$_instance instanceof self)) {
            self::$_instance = new self();
        }
        return self::$_instance;
    }

    /**
     * 获取数据
     * @param $condition
     * @param array $field
     * @return mixed
     */
    public  function getList($condition,array $field = self::_field)
    {
        return DB::table(self::table)->select($field)->where($condition)->get()->toarray();
    }

    /**
     * 获取分页数据
     * @param $condition
     * @param $page
     * @param $limit
     * @param array $field
     * @return mixed
     */
    public  function getPageList($condition, $page, $limit,array $field = self::_field)
    {
        return DB::table(self::table)->select($field)->where($condition)->apiPaginate($limit, ['*'], 'page', $page)->toarray();
    }

    /**
     * 获取数据数量
     * @param $condition
     * @param array $field
     * @return int
     */
    public function getCount($condition, array $field = self::_field)
    {
        return DB::table(self::table)->select($field)->where($condition)->count();
    }

    /**
     * 获取购买奖券数量
     * @param $condition
     * @param array $field
     * @return int
     */
    public function getTicketNumSum($condition)
    {
        return DB::table(self::table)->where($condition)->sum('ticket_num');
    }

    /**
     * @param $condition
     * @return array
     */
    public  function getUserTicketNum($condition)
    {
        return DB::table(self::table)->where($condition)
            ->groupBy('uid')
            ->select(DB::raw('sum(ticket_num) AS total_num, uid'))
            ->get()->toArray();
    }

    /**
     * 抽奖记录列表--left join--用户表
     * @param $where_data
     * @param $page
     * @param $limit
     * @param $where_in_data
     * @param $select
     * @return mixed
     */
    static public function left_join_listed($where_data = [],$page, $limit,  $where_in_data = [], $select = ['*'])
    {
        $query = DB::table(self::table)
            ->leftJoin('members', 'members.uid', '=', 'activity_lifefree_log.uid')
            ->select($select)
            ->where($where_data);

        if (!empty($where_in_data)) {
            $query->whereIn('id', $where_in_data);
        }

        $data = $query->apiPaginate($limit, ['*'], 'page', $page)->toArray();
        return $data;
    }
    /**
     * 保存数据
     * @param $data
     * @return bool
     */
    public function insertSave($data)
    {
        if (!$data) {
            return '';
        }
        $data['create_time'] = time();
        return DB::table(self::table)->insertGetId($data);
    }

    /**
     * 修改数据
     * @param $condition
     * @param $data
     * @return int|string
     */
    public function updateSave($condition, $data)
    {
        if (!$condition || !$data) {
            return '';
        }
        $data['update_time'] = time();
        return DB::table(self::table)->where($condition)->update($data);
    }

}
